﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TVDL4.VO;
using System.Data;
using System.Data.SqlClient;

namespace TVDL4.DAO
{
    class CongTrinhDAO
    {
         Random rnd = new Random();
        private dbConnection conn;
        public CongTrinhDAO()
        {
            conn = new dbConnection();
        }
        /// <summary>
        /// tra ve mot thong tin vat lieu theo ten vat lieu
        /// </summary>
        /// <param name="tenCongTrinh"></param>
        /// <returns></returns>
        public DataTable SearchByName(string tenCongTrinh)
        {
            string query = string.Format("select * from DMCONGTRINH where TENCONGTRINH like @congtrinh ");
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new SqlParameter("@congtrinh", SqlDbType.NVarChar);
            sqlParameters[0].Value = Convert.ToString(tenCongTrinh);
            return conn.executeSelectQuery(query, sqlParameters);
        }
        /// <summary>
        /// lay tat ca cac du lieu tu table DMVATLIEU
        /// </summary>
        /// <returns></returns>
        public DataTable SearchAll()
        {
            string query = string.Format("select TENCONGTRINH,KEYSL,LOAICONGTRINH,HOANTHANH from DMCONGTRINH order by tencongtrinh");
            SqlParameter[] sqlParameters = new SqlParameter[0];
            return conn.executeSelectQuery(query, sqlParameters);
        }
        /// <summary>
        /// insert table DMVATLIEU
        /// </summary>
        /// <param name="tenVatLieu"></param>
        /// <returns></returns>
        public CongTrinhVO InsertRecord(CongTrinhVO congtrinhVO)
        {
            string query = string.Format("Insert into DMCONGTRINH(KEYSL,TENCONGTRINH,HOANTHANH,LOAICONGTRINH) VALUES (@keysl,@congtrinh,@hoanthanh,@loaicongtrinh)");
            SqlParameter[] sqlParameters = new SqlParameter[4];
            sqlParameters[0] = new SqlParameter("@keysl", SqlDbType.NVarChar);
            sqlParameters[0].Value = Convert.ToString(Module.CreateRunninNo(rnd));
            sqlParameters[1] = new SqlParameter("@congtrinh", SqlDbType.NVarChar);
            sqlParameters[1].Value = Convert.ToString(congtrinhVO.TenCongTrinh);
            sqlParameters[2] = new SqlParameter("@hoanthanh", SqlDbType.Bit);
            sqlParameters[2].Value = Convert.ToInt16(congtrinhVO.HoanThanh);
            sqlParameters[3] = new SqlParameter("@loaicongtrinh", SqlDbType.NVarChar);
            sqlParameters[3].Value = Convert.ToString(congtrinhVO.LoaiCongTrinh);
            if (conn.executeInsertQuery(query, sqlParameters))
            {
                congtrinhVO.KeySL = sqlParameters[0].Value.ToString();
                return congtrinhVO;
            }
            else
            {
                return null;
            }
           
        }
        /// <summary>
        /// Update Vật Liệu
        /// </summary>
        /// <param name="congtrinhVO"></param>
        /// <returns></returns>
        public bool UpdateRecord(CongTrinhVO congtrinhVO)
        {
            string query = string.Format("UPDATE DMCONGTRINH SET TENCONGTRINH = @Congtrinh,LOAICONGTRINH = @loaicongtrinh  WHERE KEYSL = @keysl");
            SqlParameter[] sqlParameters = new SqlParameter[3];
            sqlParameters[0] = new SqlParameter("@keysl", SqlDbType.NVarChar);
            sqlParameters[0].Value = Convert.ToString(congtrinhVO.KeySL);
            sqlParameters[1] = new SqlParameter("@Congtrinh", SqlDbType.NVarChar);
            sqlParameters[1].Value = Convert.ToString(congtrinhVO.TenCongTrinh);
            sqlParameters[2] = new SqlParameter("@loaicongtrinh", SqlDbType.NVarChar);
            sqlParameters[2].Value = Convert.ToString(congtrinhVO.LoaiCongTrinh);
            return conn.executeInsertQuery(query, sqlParameters);
           
        }
        /// <summary>
        /// Update Vật Liệu
        /// </summary>
        /// <param name="congtrinhVO"></param>
        /// <returns></returns>
        public bool UpdateHoanThanh(CongTrinhVO congtrinhVO)
        {
            string query = string.Format("UPDATE DMCONGTRINH SET HOANTHANH = 1 WHERE KEYSL = @keysl");
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new SqlParameter("@keysl", SqlDbType.NVarChar);
            sqlParameters[0].Value = Convert.ToString(congtrinhVO.KeySL);
            return conn.executeInsertQuery(query, sqlParameters);

        }
        /// <summary>
        /// Xóa vat liej
        /// </summary>
        /// <param name="keySL"></param>
        /// <returns></returns>
        public bool DeleteRecord(string keySL)
        {
            string query = string.Format("Delete DMCONGTRINH Where KEYSL = @keysl");
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new SqlParameter("@keysl", SqlDbType.NVarChar);
            sqlParameters[0].Value = Convert.ToString(keySL);
            return conn.executeInsertQuery(query, sqlParameters);

        }
    }
}
